<?php

define("TEMPLATE_DIR", "templates/");

class TemplateHandler
{
	private $exists = false;
	
	private $template;
	
	private $protected_vars = array();
	
	public function __construct($template)
	{
		if(!file_exists(TEMPLATE_DIR.$template))
			return false;
		
		$this->protected_vars = get_class_vars("TemplateHandler");
		$this->template = TEMPLATE_DIR.$template;
		$this->exists = true;
		
		return true;
	}
	
	public function display()
	{
		if($this->exists)
			require $this->template;
		
		return false;
	}
	
	public function source()
	{
		if($this->exists)
		{
			$source = "";
			
			$fp = fopen($this->template, "r");
			while(($line = fgets($fp)))
			{
				$source .= $line;
			}
			fclose($fp);
			
			return $source;
		}
		
		return false;
	}
	
	public function assign($key, $value)
	{
		if(!in_array($key, $this->protected_vars) && is_string($key))
		{
			$this->$key = $value;
			return true;
		}
		
		return false;
	}
	
	public function get($key)
	{
		if(!in_array($key, $this->protected_vars) && is_string($key))
			return $this->$key;
		
		return false;
	}
}

?>